package com.flipdf2.serialize.ppml.element;

import java.awt.geom.Rectangle2D;
import java.net.URI;

import org.jdom.Element;
import org.jdom.Namespace;

import com.flipdf2.serialize.ppml.PPMLException;
import com.flipdf2.serialize.ppml.AttributeType.PPMLIndexRange;
import com.flipdf2.serialize.ppml.AttributeType.PPMLWeight;

public class PPMLSegmentArray {
	public static final String NAME = "SEGMENT_ARRAY";
	
	private PPMLView view;
	
	private PPMLExternalData externalData;
	private PPMLInternalData internalData;
	
	private Rectangle2D.Double clippingBox;
	private double[][] dimensions;
	private String environment;
	private String format;
	private PPMLIndexRange indexRange;
	private String name;
	private String overwrite;
	private String scope;
	private URI src;
	private String checksum;
	private String checksumType = "MD5";
	private int weight;
	
	public Element toXML(Namespace defaultNamespace) {
		Element root = null;
		if (defaultNamespace != null) {
			root = new Element(NAME, defaultNamespace);
		} else {
			root = new Element(NAME);	
		}
		
		if (view != null)
			root.addContent(view.toXML(defaultNamespace));
		
		if (externalData != null)
			root.addContent(externalData.toXML(defaultNamespace));
		
		if (internalData != null)
			root.addContent(internalData.toXML(defaultNamespace));
		
		if (clippingBox != null) {
			root.setAttribute("ClippingBox", clippingBox.x + " " + clippingBox.y
				+ " " + clippingBox.width + " " + clippingBox.height);
		}
		
		if (dimensions != null)
			root.setAttribute("Dimensions", dimensions[0] + " " + dimensions[1]);
		if (environment != null) 
			root.setAttribute("Environment", environment);
		if (format != null) 
			root.setAttribute("Format", format);
		if (indexRange != null) 
			root.setAttribute("IndexRange", indexRange.toString());
		if (name != null) 
			root.setAttribute("Name", name);
		if (overwrite != null) 
			root.setAttribute("Overwrite", overwrite);
		if (scope != null) 
			root.setAttribute("Scope", scope);
		if (src != null) 
			root.setAttribute("Src", src.toString());
		if (checksum != null) 
			root.setAttribute("Checksum", checksum);
		if (checksumType != null) 
			root.setAttribute("ChecksumType", checksumType);
		if (PPMLWeight.isValid(weight)) 
			root.setAttribute("Weight", weight + "");
		
		return root;
	}

	public void check() throws PPMLException {
		if (dimensions == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "Dimensions");
		}
		if (format == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "Format");
		}
		if (name == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "Name");
		}
		if (scope!= null && scope.equals("Global")) {
			if (environment == null) {
				throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "Environment");
			}
		}
		if (indexRange == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "IndexRange");
		}
	}
	
	public String getChecksum() {
		return checksum;
	}

	public void setChecksum(String checksum) {
		this.checksum = checksum;
	}

	public String getChecksumType() {
		return checksumType;
	}

	public void setChecksumType(String checksumType) {
		this.checksumType = checksumType;
	}

	public Rectangle2D.Double getClippingBox() {
		return clippingBox;
	}

	public void setClippingBox(Rectangle2D.Double clippingBox) {
		this.clippingBox = clippingBox;
	}

	public double[][] getDimensions() {
		return dimensions;
	}

	public void setDimensions(double[][] dimensions) {
		this.dimensions = dimensions;
	}

	public String getEnvironment() {
		return environment;
	}

	public void setEnvironment(String environment) {
		this.environment = environment;
	}

	public PPMLExternalData getExternalData() {
		return externalData;
	}

	public void setExternalData(PPMLExternalData externalData) {
		this.externalData = externalData;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public PPMLIndexRange getIndexRange() {
		return indexRange;
	}

	public void setIndexRange(PPMLIndexRange indexRange) {
		this.indexRange = indexRange;
	}

	public PPMLInternalData getInternalData() {
		return internalData;
	}

	public void setInternalData(PPMLInternalData internalData) {
		this.internalData = internalData;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getOverwrite() {
		return overwrite;
	}

	public void setOverwrite(String overwrite) {
		this.overwrite = overwrite;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public URI getSrc() {
		return src;
	}

	public void setSrc(URI src) {
		this.src = src;
	}

	public PPMLView getView() {
		return view;
	}

	public void setView(PPMLView view) {
		this.view = view;
	}

	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}
}
